{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n",
      "c:\\users\\terry\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\tensorflow\\python\\framework\\dtypes.py:516: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_qint8 = np.dtype([(\"qint8\", np.int8, 1)])\n",
      "c:\\users\\terry\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\tensorflow\\python\\framework\\dtypes.py:517: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_quint8 = np.dtype([(\"quint8\", np.uint8, 1)])\n",
      "c:\\users\\terry\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\tensorflow\\python\\framework\\dtypes.py:518: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_qint16 = np.dtype([(\"qint16\", np.int16, 1)])\n",
      "c:\\users\\terry\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\tensorflow\\python\\framework\\dtypes.py:519: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_quint16 = np.dtype([(\"quint16\", np.uint16, 1)])\n",
      "c:\\users\\terry\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\tensorflow\\python\\framework\\dtypes.py:520: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_qint32 = np.dtype([(\"qint32\", np.int32, 1)])\n",
      "c:\\users\\terry\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\tensorflow\\python\\framework\\dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  np_resource = np.dtype([(\"resource\", np.ubyte, 1)])\n",
      "c:\\users\\terry\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\tensorboard\\compat\\tensorflow_stub\\dtypes.py:541: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_qint8 = np.dtype([(\"qint8\", np.int8, 1)])\n",
      "c:\\users\\terry\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\tensorboard\\compat\\tensorflow_stub\\dtypes.py:542: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_quint8 = np.dtype([(\"quint8\", np.uint8, 1)])\n",
      "c:\\users\\terry\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\tensorboard\\compat\\tensorflow_stub\\dtypes.py:543: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_qint16 = np.dtype([(\"qint16\", np.int16, 1)])\n",
      "c:\\users\\terry\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\tensorboard\\compat\\tensorflow_stub\\dtypes.py:544: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_quint16 = np.dtype([(\"quint16\", np.uint16, 1)])\n",
      "c:\\users\\terry\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\tensorboard\\compat\\tensorflow_stub\\dtypes.py:545: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_qint32 = np.dtype([(\"qint32\", np.int32, 1)])\n",
      "c:\\users\\terry\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\tensorboard\\compat\\tensorflow_stub\\dtypes.py:550: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  np_resource = np.dtype([(\"resource\", np.ubyte, 1)])\n"
     ]
    }
   ],
   "source": [
    "import nltk\n",
    "from nltk.stem import WordNetLemmatizer\n",
    "lemmatizer = WordNetLemmatizer()\n",
    "import json\n",
    "import pickle\n",
    "import numpy as np\n",
    "from keras.models import Sequential\n",
    "from keras.layers import Dense,Activation,Dropout\n",
    "from keras.optimizers import SGD\n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "newdict={}\n",
    "newdict[\"intents\"]=[\n",
    "    {\"tag\":\"greeting\",\n",
    "    \"patterns\":[\"hi\",\"hello\",\"hi there\",\"hey\",\"are you there?\",\"yo\",\"whats up\"],\n",
    "    \"responses\":[\"Hello\",\"Hi\",\"How can i help?\",\"Hey\"],\n",
    "    \"context\":[\"\"]\n",
    "    },\n",
    "    {\"tag\":\"goodbye\",\n",
    "    \"patterns\":[\"bye\",\"see ya later\",\"goodbye\",\"ok bye\"],\n",
    "    \"responses\":[\"Bye\",\"Goodbye\",\"Come back again soon\",\"Nice to meet you.Bye!\"],\n",
    "    \"context\":[\"\"]\n",
    "    },\n",
    "    {\"tag\":\"noanswer\",\n",
    "    \"patterns\":[],\n",
    "    \"responses\":[\"Please give me more info\",\"Sorry i cant understand you\"],\n",
    "    \"context\":[\"\"]\n",
    "    },\n",
    "    {\"tag\":\"job\",\n",
    "    \"patterns\":[\"what you do?\",\"how can u help me?\",\"what are you gonna do\"],\n",
    "    \"responses\":[\"I can guide u through Artificial Intelligence(AI),Data Science and Natural Language Processing\"],\n",
    "    \"context\":[\"\"]\n",
    "    },\n",
    "    {\"tag\":\"thanks\",\n",
    "    \"patterns\":[\"thanks\",\"thank you\",\"thanks for the help\",\"thats helpful\"],\n",
    "    \"responses\":[\"My Pleasure\",\"You are Welcome\",\"Happy to help :)\"],\n",
    "    \"context\":[\"\"]\n",
    "    }\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'intents': [{'tag': 'greeting', 'patterns': ['hi', 'hello', 'hi there', 'hey', 'are you there?', 'yo', 'whats up'], 'responses': ['Hello', 'Hi', 'How can i help?', 'Hey'], 'context': ['']}, {'tag': 'goodbye', 'patterns': ['bye', 'see ya later', 'goodbye', 'ok bye'], 'responses': ['Bye', 'Goodbye', 'Come back again soon', 'Nice to meet you.Bye!'], 'context': ['']}, {'tag': 'noanswer', 'patterns': [], 'responses': ['Please give me more info', 'Sorry i cant understand you'], 'context': ['']}, {'tag': 'job', 'patterns': ['what you do?', 'how can u help me?', 'what are you gonna do'], 'responses': ['I can guide u through Artificial Intelligence(AI),Data Science and Natural Language Processing'], 'context': ['']}, {'tag': 'thanks', 'patterns': ['thanks', 'thank you', 'thanks for the help', 'thats helpful'], 'responses': ['My Pleasure', 'You are Welcome', 'Happy to help :)'], 'context': ['']}]}\n"
     ]
    }
   ],
   "source": [
    "print(newdict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "intents=json.dumps(newdict)\n",
    "with open(\"intents.json\",\"w\") as f:\n",
    "    f.write(intents)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "words=[]\n",
    "classes=[]\n",
    "documents=[]\n",
    "ignore_words=['?','!']\n",
    "data_file=open('intents.json').read()\n",
    "intents=json.loads(data_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "greeting\n",
      "goodbye\n",
      "noanswer\n",
      "job\n",
      "thanks\n"
     ]
    }
   ],
   "source": [
    "for intent in intents['intents']:\n",
    "    print(intent['tag'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "for intent in intents['intents']:\n",
    "    for pattern in intent['patterns']:\n",
    "        #tokenize each word\n",
    "        w=nltk.word_tokenize(pattern)\n",
    "        words.extend(w)\n",
    "        #add tags in corpus\n",
    "        documents.append((w,intent['tag']))\n",
    "        #add classes to our tags\n",
    "        if intent['tag'] not in classes:\n",
    "            classes.append(intent['tag'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18 documents\n",
      "4 classes ['goodbye', 'greeting', 'job', 'thanks']\n",
      "30 unique lemmatized words ['are', 'bye', 'can', 'do', 'for', 'gon', 'goodbye', 'hello', 'help', 'helpful', 'hey', 'hi', 'how', 'later', 'me', 'na', 'ok', 'see', 'thank', 'thanks', 'thats', 'the', 'there', 'u', 'up', 'what', 'whats', 'ya', 'yo', 'you']\n"
     ]
    }
   ],
   "source": [
    "#lemmatize each word and remove duplicates\n",
    "words=[lemmatizer.lemmatize(w.lower()) for w in words if w not in ignore_words]\n",
    "words=sorted(list(set(words)))\n",
    "#sort classes\n",
    "classes=sorted(list(set(classes)))\n",
    "#documents = combination between patterns and intents\n",
    "print(len(documents), \"documents\")\n",
    "#classes =intents\n",
    "print (len(classes), \"classes\", classes)\n",
    "#words =all words,vocabulary\n",
    "print(len(words), \"unique lemmatized words\", words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "#store it in pickle\n",
    "pickle.dump(words,open('words.pkl','wb'))\n",
    "pickle.dump(classes,open('classes.pkl','wb'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "#create training data\n",
    "training=[]\n",
    "output_empty=[0]*len(classes)\n",
    "for doc in documents:\n",
    "    bag=[]\n",
    "    pattern_words=doc[0]\n",
    "    pattern_words=[lemmatizer.lemmatize(word.lower()) for word in pattern_words]\n",
    "    # create our bag of words array with 1, if word match found in current pattern\n",
    "    for w in words:\n",
    "        bag.append(1) if w in pattern_words else bag.append(0)\n",
    "    # output is a '0' for each tag and '1' for current tag (for each pattern)\n",
    "    output_row=list(output_empty)\n",
    "    output_row[classes.index(doc[1])]=1\n",
    "    training.append([bag,output_row])\n",
    "# shuffle our features and turn into np.array])\n",
    "random.shuffle(training)\n",
    "training = np.array(training)\n",
    "# create train and test lists. X - patterns, Y - intents\n",
    "train_x=list(training[:,0])\n",
    "train_y=list(training[:,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/100\n",
      "18/18 [==============================] - 0s 14ms/step - loss: 1.3894 - accuracy: 0.2222\n",
      "Epoch 2/100\n",
      "18/18 [==============================] - 0s 1ms/step - loss: 1.3263 - accuracy: 0.3889\n",
      "Epoch 3/100\n",
      "18/18 [==============================] - 0s 754us/step - loss: 1.2709 - accuracy: 0.5000\n",
      "Epoch 4/100\n",
      "18/18 [==============================] - 0s 471us/step - loss: 1.2042 - accuracy: 0.6667\n",
      "Epoch 5/100\n",
      "18/18 [==============================] - 0s 412us/step - loss: 1.2670 - accuracy: 0.4444\n",
      "Epoch 6/100\n",
      "18/18 [==============================] - 0s 509us/step - loss: 1.1501 - accuracy: 0.5556\n",
      "Epoch 7/100\n",
      "18/18 [==============================] - 0s 604us/step - loss: 1.2492 - accuracy: 0.6111\n",
      "Epoch 8/100\n",
      "18/18 [==============================] - 0s 851us/step - loss: 1.0966 - accuracy: 0.6667\n",
      "Epoch 9/100\n",
      "18/18 [==============================] - 0s 356us/step - loss: 1.0270 - accuracy: 0.6667\n",
      "Epoch 10/100\n",
      "18/18 [==============================] - 0s 532us/step - loss: 1.0291 - accuracy: 0.5556\n",
      "Epoch 11/100\n",
      "18/18 [==============================] - 0s 502us/step - loss: 0.9920 - accuracy: 0.6111\n",
      "Epoch 12/100\n",
      "18/18 [==============================] - 0s 409us/step - loss: 0.8987 - accuracy: 0.6111\n",
      "Epoch 13/100\n",
      "18/18 [==============================] - 0s 425us/step - loss: 0.7854 - accuracy: 0.8889\n",
      "Epoch 14/100\n",
      "18/18 [==============================] - 0s 540us/step - loss: 0.9110 - accuracy: 0.6667\n",
      "Epoch 15/100\n",
      "18/18 [==============================] - 0s 502us/step - loss: 0.7593 - accuracy: 0.8333\n",
      "Epoch 16/100\n",
      "18/18 [==============================] - 0s 527us/step - loss: 0.9126 - accuracy: 0.6111\n",
      "Epoch 17/100\n",
      "18/18 [==============================] - 0s 495us/step - loss: 0.6861 - accuracy: 0.8333\n",
      "Epoch 18/100\n",
      "18/18 [==============================] - 0s 395us/step - loss: 0.6877 - accuracy: 0.7222\n",
      "Epoch 19/100\n",
      "18/18 [==============================] - 0s 436us/step - loss: 0.6244 - accuracy: 0.8889\n",
      "Epoch 20/100\n",
      "18/18 [==============================] - 0s 714us/step - loss: 0.5526 - accuracy: 0.8889\n",
      "Epoch 21/100\n",
      "18/18 [==============================] - 0s 843us/step - loss: 0.5464 - accuracy: 0.8889\n",
      "Epoch 22/100\n",
      "18/18 [==============================] - 0s 778us/step - loss: 0.4123 - accuracy: 0.9444\n",
      "Epoch 23/100\n",
      "18/18 [==============================] - 0s 681us/step - loss: 0.3871 - accuracy: 1.0000\n",
      "Epoch 24/100\n",
      "18/18 [==============================] - 0s 596us/step - loss: 0.3978 - accuracy: 0.8333\n",
      "Epoch 25/100\n",
      "18/18 [==============================] - 0s 602us/step - loss: 0.4537 - accuracy: 0.8889\n",
      "Epoch 26/100\n",
      "18/18 [==============================] - 0s 400us/step - loss: 0.2995 - accuracy: 0.9444\n",
      "Epoch 27/100\n",
      "18/18 [==============================] - 0s 348us/step - loss: 0.3712 - accuracy: 0.8889\n",
      "Epoch 28/100\n",
      "18/18 [==============================] - 0s 550us/step - loss: 0.4396 - accuracy: 0.8889\n",
      "Epoch 29/100\n",
      "18/18 [==============================] - 0s 589us/step - loss: 0.3180 - accuracy: 0.9444\n",
      "Epoch 30/100\n",
      "18/18 [==============================] - 0s 539us/step - loss: 0.2237 - accuracy: 0.9444\n",
      "Epoch 31/100\n",
      "18/18 [==============================] - 0s 999us/step - loss: 0.3268 - accuracy: 0.9444\n",
      "Epoch 32/100\n",
      "18/18 [==============================] - 0s 994us/step - loss: 0.2005 - accuracy: 1.0000\n",
      "Epoch 33/100\n",
      "18/18 [==============================] - 0s 831us/step - loss: 0.1480 - accuracy: 1.0000\n",
      "Epoch 34/100\n",
      "18/18 [==============================] - 0s 897us/step - loss: 0.3560 - accuracy: 0.9444\n",
      "Epoch 35/100\n",
      "18/18 [==============================] - 0s 797us/step - loss: 0.2725 - accuracy: 0.8889\n",
      "Epoch 36/100\n",
      "18/18 [==============================] - 0s 890us/step - loss: 0.1127 - accuracy: 1.0000\n",
      "Epoch 37/100\n",
      "18/18 [==============================] - 0s 781us/step - loss: 0.1435 - accuracy: 1.0000\n",
      "Epoch 38/100\n",
      "18/18 [==============================] - 0s 588us/step - loss: 0.1039 - accuracy: 0.9444\n",
      "Epoch 39/100\n",
      "18/18 [==============================] - 0s 575us/step - loss: 0.0864 - accuracy: 1.0000\n",
      "Epoch 40/100\n",
      "18/18 [==============================] - 0s 559us/step - loss: 0.1373 - accuracy: 1.0000\n",
      "Epoch 41/100\n",
      "18/18 [==============================] - 0s 525us/step - loss: 0.0972 - accuracy: 0.9444\n",
      "Epoch 42/100\n",
      "18/18 [==============================] - 0s 580us/step - loss: 0.1140 - accuracy: 1.0000\n",
      "Epoch 43/100\n",
      "18/18 [==============================] - 0s 727us/step - loss: 0.2295 - accuracy: 0.9444\n",
      "Epoch 44/100\n",
      "18/18 [==============================] - 0s 457us/step - loss: 0.2270 - accuracy: 0.9444\n",
      "Epoch 45/100\n",
      "18/18 [==============================] - 0s 505us/step - loss: 0.0986 - accuracy: 1.0000\n",
      "Epoch 46/100\n",
      "18/18 [==============================] - 0s 529us/step - loss: 0.1130 - accuracy: 0.9444\n",
      "Epoch 47/100\n",
      "18/18 [==============================] - 0s 554us/step - loss: 0.1146 - accuracy: 1.0000\n",
      "Epoch 48/100\n",
      "18/18 [==============================] - 0s 569us/step - loss: 0.1388 - accuracy: 0.9444\n",
      "Epoch 49/100\n",
      "18/18 [==============================] - 0s 464us/step - loss: 0.2665 - accuracy: 0.8889\n",
      "Epoch 50/100\n",
      "18/18 [==============================] - 0s 553us/step - loss: 0.0808 - accuracy: 1.0000\n",
      "Epoch 51/100\n",
      "18/18 [==============================] - 0s 568us/step - loss: 0.0702 - accuracy: 1.0000\n",
      "Epoch 52/100\n",
      "18/18 [==============================] - 0s 531us/step - loss: 0.1031 - accuracy: 1.0000\n",
      "Epoch 53/100\n",
      "18/18 [==============================] - 0s 486us/step - loss: 0.0939 - accuracy: 0.9444\n",
      "Epoch 54/100\n",
      "18/18 [==============================] - 0s 443us/step - loss: 0.1153 - accuracy: 0.9444\n",
      "Epoch 55/100\n",
      "18/18 [==============================] - 0s 581us/step - loss: 0.0565 - accuracy: 1.0000\n",
      "Epoch 56/100\n",
      "18/18 [==============================] - 0s 518us/step - loss: 0.0529 - accuracy: 1.0000\n",
      "Epoch 57/100\n",
      "18/18 [==============================] - 0s 721us/step - loss: 0.0721 - accuracy: 1.0000\n",
      "Epoch 58/100\n",
      "18/18 [==============================] - 0s 1ms/step - loss: 0.0939 - accuracy: 1.0000\n",
      "Epoch 59/100\n",
      "18/18 [==============================] - 0s 860us/step - loss: 0.0816 - accuracy: 1.0000\n",
      "Epoch 60/100\n",
      "18/18 [==============================] - 0s 593us/step - loss: 0.1029 - accuracy: 0.9444\n",
      "Epoch 61/100\n",
      "18/18 [==============================] - 0s 439us/step - loss: 0.1352 - accuracy: 0.9444\n",
      "Epoch 62/100\n",
      "18/18 [==============================] - 0s 606us/step - loss: 0.0427 - accuracy: 1.0000\n",
      "Epoch 63/100\n",
      "18/18 [==============================] - 0s 790us/step - loss: 0.0620 - accuracy: 1.0000\n",
      "Epoch 64/100\n",
      "18/18 [==============================] - 0s 606us/step - loss: 0.0221 - accuracy: 1.0000\n",
      "Epoch 65/100\n",
      "18/18 [==============================] - 0s 724us/step - loss: 0.0525 - accuracy: 1.0000\n",
      "Epoch 66/100\n",
      "18/18 [==============================] - 0s 566us/step - loss: 0.0216 - accuracy: 1.0000\n",
      "Epoch 67/100\n",
      "18/18 [==============================] - 0s 517us/step - loss: 0.0477 - accuracy: 1.0000\n",
      "Epoch 68/100\n",
      "18/18 [==============================] - 0s 416us/step - loss: 0.0510 - accuracy: 1.0000\n",
      "Epoch 69/100\n",
      "18/18 [==============================] - 0s 585us/step - loss: 0.0190 - accuracy: 1.0000\n",
      "Epoch 70/100\n",
      "18/18 [==============================] - 0s 697us/step - loss: 0.0418 - accuracy: 1.0000\n",
      "Epoch 71/100\n",
      "18/18 [==============================] - 0s 604us/step - loss: 0.1261 - accuracy: 0.9444\n",
      "Epoch 72/100\n",
      "18/18 [==============================] - 0s 585us/step - loss: 0.0166 - accuracy: 1.0000\n",
      "Epoch 73/100\n",
      "18/18 [==============================] - 0s 569us/step - loss: 0.0309 - accuracy: 1.0000\n",
      "Epoch 74/100\n",
      "18/18 [==============================] - 0s 497us/step - loss: 0.1630 - accuracy: 1.0000\n",
      "Epoch 75/100\n",
      "18/18 [==============================] - 0s 494us/step - loss: 0.0408 - accuracy: 1.0000\n",
      "Epoch 76/100\n",
      "18/18 [==============================] - 0s 530us/step - loss: 0.1157 - accuracy: 0.9444\n",
      "Epoch 77/100\n",
      "18/18 [==============================] - 0s 514us/step - loss: 0.0560 - accuracy: 1.0000\n",
      "Epoch 78/100\n",
      "18/18 [==============================] - 0s 521us/step - loss: 0.0334 - accuracy: 1.0000\n",
      "Epoch 79/100\n",
      "18/18 [==============================] - 0s 628us/step - loss: 0.0427 - accuracy: 1.0000\n",
      "Epoch 80/100\n",
      "18/18 [==============================] - 0s 556us/step - loss: 0.0348 - accuracy: 1.0000\n",
      "Epoch 81/100\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18/18 [==============================] - 0s 499us/step - loss: 0.1099 - accuracy: 1.0000\n",
      "Epoch 82/100\n",
      "18/18 [==============================] - 0s 450us/step - loss: 0.0202 - accuracy: 1.0000\n",
      "Epoch 83/100\n",
      "18/18 [==============================] - 0s 444us/step - loss: 0.0400 - accuracy: 1.0000\n",
      "Epoch 84/100\n",
      "18/18 [==============================] - 0s 575us/step - loss: 0.1038 - accuracy: 0.9444\n",
      "Epoch 85/100\n",
      "18/18 [==============================] - 0s 485us/step - loss: 0.0583 - accuracy: 0.9444\n",
      "Epoch 86/100\n",
      "18/18 [==============================] - 0s 346us/step - loss: 0.0211 - accuracy: 1.0000\n",
      "Epoch 87/100\n",
      "18/18 [==============================] - 0s 805us/step - loss: 0.0565 - accuracy: 1.0000\n",
      "Epoch 88/100\n",
      "18/18 [==============================] - 0s 934us/step - loss: 0.0425 - accuracy: 1.0000\n",
      "Epoch 89/100\n",
      "18/18 [==============================] - 0s 866us/step - loss: 0.0317 - accuracy: 1.0000\n",
      "Epoch 90/100\n",
      "18/18 [==============================] - 0s 672us/step - loss: 0.0221 - accuracy: 1.0000\n",
      "Epoch 91/100\n",
      "18/18 [==============================] - 0s 733us/step - loss: 0.1730 - accuracy: 0.9444\n",
      "Epoch 92/100\n",
      "18/18 [==============================] - 0s 595us/step - loss: 0.0289 - accuracy: 1.0000\n",
      "Epoch 93/100\n",
      "18/18 [==============================] - 0s 430us/step - loss: 0.0280 - accuracy: 1.0000\n",
      "Epoch 94/100\n",
      "18/18 [==============================] - 0s 426us/step - loss: 0.0102 - accuracy: 1.0000\n",
      "Epoch 95/100\n",
      "18/18 [==============================] - 0s 634us/step - loss: 0.0092 - accuracy: 1.0000\n",
      "Epoch 96/100\n",
      "18/18 [==============================] - 0s 519us/step - loss: 0.0142 - accuracy: 1.0000\n",
      "Epoch 97/100\n",
      "18/18 [==============================] - 0s 423us/step - loss: 0.0197 - accuracy: 1.0000\n",
      "Epoch 98/100\n",
      "18/18 [==============================] - 0s 447us/step - loss: 0.0145 - accuracy: 1.0000\n",
      "Epoch 99/100\n",
      "18/18 [==============================] - 0s 454us/step - loss: 0.0641 - accuracy: 1.0000\n",
      "Epoch 100/100\n",
      "18/18 [==============================] - 0s 371us/step - loss: 0.0080 - accuracy: 1.0000\n",
      "model created\n"
     ]
    }
   ],
   "source": [
    "# Create model - 3 layers. First layer 128 neurons, second layer 64 neurons and 3rd output layer contains number of neurons\n",
    "# equal to number of intents to predict output intent with softmax\n",
    "model=Sequential()\n",
    "model.add(Dense(128,input_shape=(len(train_x[0]),), activation='relu'))\n",
    "model.add(Dropout(0.5))\n",
    "model.add(Dense(64,activation='relu'))\n",
    "model.add(Dropout(0.5))\n",
    "model.add(Dense(len(train_y[0]),activation='softmax'))\n",
    "sgd=SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)\n",
    "model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])\n",
    "#fitting and saving the model \n",
    "hist=model.fit(np.array(train_x),np.array(train_y),epochs=100,batch_size=5,verbose=1)\n",
    "model.save('chatbot_model.h5',hist)\n",
    "print(\"model created\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
